package com.zhou.juc.test10_AQS;

import java.util.concurrent.locks.AbstractQueuedSynchronizer;

/**
 * @author zhouyanxiang
 * @create 2020-08-2020/8/16-15:41
 */
public class Sync extends AbstractQueuedSynchronizer {

    @Override
    protected boolean tryAcquire(int arg){
        if (compareAndSetState(0,1)){
            setExclusiveOwnerThread(Thread.currentThread());
            return true;
        }else {
            return false;
        }
    }

    @Override
    protected boolean tryRelease(int arg){
        setExclusiveOwnerThread(null);
        setState(0);
        return true;
    }

    @Override
    protected boolean isHeldExclusively(){
        return getState() == 1;
    }
}
